package com.situ.skymall.user.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.model.UserAddress;
import com.situ.service.UserAddressService;
import com.situ.skymall.user.api.mapper.UserMapper;
import com.situ.model.User;
import com.situ.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private UserMapper userMapper;
    private UserAddressService userAddressService;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }


    @Autowired
    public void setUserAddressService(UserAddressService userAddressService) {
        this.userAddressService = userAddressService;
    }

    @Override
    public Page<User> findAll(int pageNum, int pageSize, User search) {
        Page<User> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (search != null) {
            if (StringUtils.hasText(search.getUsername())) {
                wrapper.like(User::getUsername, search.getUsername());
            }
            if (StringUtils.hasText(search.getPhone())) {
                wrapper.like(User::getPhone, search.getPhone());
            }
            if (search.getRoleType() != null) {
                wrapper.eq(User::getRoleType, search.getRoleType());
            }
            if (StringUtils.hasText(search.getNickname())) {
                wrapper.like(User::getNickname, search.getNickname());
            }
        }
        // 过滤掉已被删除的用户
        wrapper.eq(User::getIsDelete, 0);

        Page<User> pageResult = userMapper.selectPage(page, wrapper);

        for(User user : pageResult.getRecords()){
            patch(user);
        }
        return page;
    }

    // 填充用户信息
    private void patch(User user){
        List<UserAddress> userAddresses = userAddressService.findByUserId(user.getId());
        if(userAddresses != null){
            user.setUserAddresses(userAddresses);
        }
    }

    @Override
    public User findByUsername(String username) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        // 过滤掉已被删除的用户
        wrapper.eq(User::getIsDelete, 0);
        return this.getOne(wrapper);
    }

    @Override
    public boolean updateStatus(Integer id, Integer status) {
        User user = this.getById(id);
        if (user != null && user.getIsDelete() == 0) { // 只有未被删除的用户才能更新状态
            user.setStatus(status);
            return this.updateById(user);
        }
        return false;
    }


    @Override
    public boolean updateRoleType(Integer id, Integer roleType) {
        User user = this.getById(id);
        if (user != null && user.getIsDelete() == 0) { // 只有未被删除的用户才能更新角色类型
            user.setRoleType(roleType);
            return this.updateById(user);
        }
        return false;
    }


    @Override
    public boolean deleteUser(Integer id) {
        User user = this.getById(id);
        if (user != null && user.getIsDelete() == 0) { // 只有未被删除的用户才能删除
            user.setIsDelete(1);
            return this.updateById(user);
        }
        return false;
    }
}